package com.cxyzy.demo

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.cxyzy.commonviewholderlib.MultipleCommonAdapter
import com.cxyzy.commonviewholderlib.ViewHolder
import java.util.*

class MainActivity : AppCompatActivity() {

    private val data = ArrayList<String>(100)
    private var context: Context? = null

    private val pageTitles = arrayOf("Page 1", "Page 2", "Page 3", "Page 4", "Page 5")

    private val multipleAdapter: RecyclerView.Adapter<*>
        get() = object : MultipleCommonAdapter<String>(this,data) {

            override fun getLayoutIdByType(viewType: Int): Int {
                return R.layout.vp
            }

            override fun getItemViewType(position: Int): Int {
                return 0
            }

            override fun onBindViewHolder(holder: ViewHolder, data: String, position: Int) {
                holder.getView<ViewPager>(R.id.viewPager).adapter = object : PagerAdapter() {
                    override fun instantiateItem(container: ViewGroup, position: Int): Any {
                        val inflater = LayoutInflater.from(this@MainActivity)
                        val rootView = inflater.inflate(R.layout.view_page, container, false) as ViewGroup
                        val textView = rootView.findViewById<TextView>(R.id.textView)
                        textView.text = getPageTitle(position)
                        container.addView(rootView)
                        return rootView
                    }

                    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
                        container.removeView(obj as View)
                    }

                    override fun getPageTitle(position: Int): CharSequence? {
                        return pageTitles[position]
                    }

                    override fun getCount(): Int {
                        return pageTitles.size
                    }

                    override fun isViewFromObject(view: View, obj: Any): Boolean {
                        return view === obj
                    }
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.context = this
        for (i in 0..99) {
            data.add("text-$i")
        }

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        recyclerView.adapter = multipleAdapter    // 使用多布局
    }

}